package com.feicheng.controller.shop.wx.app.client;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.service.IFPayService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/wx/app/client/card-record")
@RequestMapping("/wx/app/client/card-record")
public class CardRecordController {
	@Resource(name="pay-service")
    Map<String, IFPayService> payServiceMap;

    @PostMapping("/info")
    public ResultInfo prePayCard(
            Integer clientId,
            Integer storeId,
            String openId,
            Integer cardTypeId,
            Integer amount) {

        return payServiceMap.get(Constant.PAY_TYPE.WX).cardPay(clientId, storeId, amount, openId, cardTypeId);
    }

    @PostMapping("/notice-url/{storeId}/{payId}/{cardTypeId}")
    public JSONObject noticePayCard(
    		@RequestHeader("Wechatpay-Nonce") String nonce,
    		@RequestHeader("Wechatpay-Signature") String signature,
    		@RequestHeader("Wechatpay-Signature-Type") String signatureType,
    		@RequestHeader("Wechatpay-Timestamp") String timestamp,
    		@RequestHeader("Wechatpay-Serial") String serial,
            @RequestBody String body,
            @PathVariable(name = "storeId") Integer storeId,
            @PathVariable(name = "payId") Integer payId,
            @PathVariable(name = "cardTypeId") Integer cardTypeId
        ) {
        payServiceMap.get(Constant.PAY_TYPE.WX)
        		.cardNotice(nonce, signature, signatureType, timestamp, serial, body, storeId, payId, cardTypeId);

        JSONObject result = new JSONObject();
        result.put("code", "SUCCESS");
        result.put("message", "成功");
        return result;
    }
}
